// 实现这个项目的构建任务
const sass = require('sass')

const cwd = process.cwd()
const path = require('path')
let defaultConfig = {
  // default config
  build: {
    src: 'src',
    dist: 'dist',
    temp: 'temp',
    public: 'public',
    paths: {
      styles: 'assets/styles/*.scss',
      scripts: 'assets/scripts/*.js',
      pages: '*.html',
      images: 'assets/images/*.{png,jpg,jpeg,gif,svg}',
      fonts: 'assets/fonts/**'
    }
  }
}
let config
try {
  const loadConfig = require(path.join(cwd, 'pages.config.js'))
  config = new Proxy(defaultConfig, {
    set: function (target, name, value) {
      return target[name] = Object.assign({}, target[name], value)
    }
  })
  Object.assign(config, loadConfig)
} catch (e) {}

module.exports = grunt => {
  require('load-grunt-tasks')(grunt)

  grunt.initConfig({
    // 设置任务，删除文件夹
    clean: [config.build.dist, config.build.temp],

    sass: {
      options: {
        implementation: sass
      },
      main: {
        files: [{
          expand: true,
          cwd: config.build.src,
          src: [config.build.paths.styles],
          dest: config.build.temp,
          ext: '.css'
        }]
      }
    },

    babel: {
      options: {
        presets: ['@babel/preset-env']
      },
      main: {
        files: [{
          expand: true,
          cwd: config.build.src,
          src: [config.build.paths.scripts],
          dest: config.build.temp,
          ext: '.js'
        }]
      }
    },

    web_swig: {
      options: {
        swigOptions: {
          cache: false
        },
        getData () {
          return config.data
        }
      },
      main: {
        files: [{
          expand: true,
          cwd: config.build.src,
          src: [config.build.paths.pages],
          dest: config.build.temp,
          ext: '.html'
        }]
      }
    },

    useref: {
      html: path.join(config.build.dist, config.build.paths.pages),
      temp: config.build.dist
    },

    imagemin: {
      main: {
        files: [{
          expand: true,
          cwd: config.build.src,
          src: [config.build.paths.images],
          dest: config.build.dist,
        }]
      }
    },

    copy: {
      temp: {
        files: [{
          expand: true,
          cwd: config.build.temp,
          src: ['**'],
          dest: config.build.dist,
        }]
      },
      fonts: {
        files: [{
          expand: true,
          cwd: config.build.src,
          src: [config.build.paths.fonts],
          dest: config.build.dist,
        }]
      },
      public: {
        files: [{
          expand: true,
          cwd: config.build.public,
          src: ['**'],
          dest: config.build.dist,
        }]
      }
    },

    watch: {
      js: {
        files: [path.join(config.build.src, config.build.paths.scripts)],
        tasks: ['babel']
      },
      css: {
        files: [path.join(config.build.src, config.build.paths.styles)],
        tasks: ['sass']
      },
      page: {
        files: [path.join(config.build.src, config.build.paths.pages)],
        tasks: ['web_swig']
      },
      image: {
        files: [path.join(config.build.src, config.build.paths.images)],
        tasks: ['imagemin']
      },
      font: {
        files: [path.join(config.build.src, config.build.paths.fonts)],
        tasks: ['copy:fonts']
      },
      extra: {
        files: [path.join(config.build.public, '**')],
        tasks: ['copy:public']
      }
    },

    browserSync: {
      dev: {
        bsFiles: {
          src: [config.build.temp, config.build.dist]
        },
        options: {
          watchTask: true,
          server: {
            baseDir: [config.build.temp, config.build.src, config.build.public],
            // 处理第三方库文件（因为我们还未处理第三方库到我们的 dist 目录下，比如 bootstrap，jquery）
            routes: {
              // 请求的前缀       目录
              '/node_modules': 'node_modules'
            }
          }
        }
      }
    }

  })
  grunt.registerTask('compile', ['sass', 'babel', 'web_swig'])

  grunt.registerTask('develop', ['compile', 'browserSync', 'watch'])

  grunt.registerTask('build', ['clean', 'compile', 'copy', 'useref', 'concat', 'uglify', 'cssmin', 'imagemin'])

}
